class Book < ActiveRecord::Base
  attr_accessible :book_name, :book_type

  #交集
  def self.intersect(part_a,part_b)
    word_a = self.get_part_words(part_a).uniq
    word_b = self.get_part_words(part_b).uniq
    
    return word_a & word_b
  end

  def self.union(part_a,part_b)
    word_a = self.get_part_words(part_a).uniq
    word_b = self.get_part_words(part_b).uniq
    return word_a | word_b
  end

  def self.subtract(part_a,part_b)
    word_a = self.get_part_words(part_a).uniq
    word_b = self.get_part_words(part_b).uniq
    return word_a - (word_a & word_b)
  end
  def self.difference(part_a,part_b)
    word_a = self.get_part_words(part_a).uniq
    word_b = self.get_part_words(part_b).uniq
    return (word_a | word_b) - (word_a & word_b)
  end


  def self.get_part_words(part)
    words_a = []
    return words_a unless part.is_a?Array
    part.each do |id|
      words_a = words_a + Word.get_words(id)
    end
    return words_a
  end
end
